﻿using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 配置表管理器
 */
namespace W.GameFrameWork.ExcelTool
{
	public class DBManager
	{
		private static DBManager _instance;

		public static DBManager Instance
		{
			get
			{
				if (_instance == null)
					_instance = new DBManager();

				return _instance;
			}
		}

		/// <summary>
		/// 项目中所有加载的csv配置表
		/// </summary>
		private Dictionary<string, List<ExcelItem>> csvTableDict = new Dictionary<string, List<ExcelItem>>();

		/// <summary>
		/// 项目中所有加载的xml配置表
		/// </summary>
		private Dictionary<string, List<ExcelItem>> xmlTableDict = new Dictionary<string, List<ExcelItem>>();

		/// <summary>
		/// 项目中所有加载的json配置表
		/// </summary>
		private Dictionary<string, List<ExcelItem>> jsonTableDict = new Dictionary<string, List<ExcelItem>>();

		/// <summary>
		/// 项目中所有加载的Lua配置表
		/// </summary>
		private Dictionary<string, List<ExcelItem>> luaTableDict = new Dictionary<string, List<ExcelItem>>();

		/// <summary>
		/// 项目中从数据库文件中加载的表数据
		/// </summary>
		private Dictionary<string, List<ExcelItem>> sqlTableDict = new Dictionary<string, List<ExcelItem>>();

		public DBManager()
		{
			if (csvTableDict == null)
				csvTableDict = new Dictionary<string, List<ExcelItem>>();

			csvTableDict.Clear();

			if (xmlTableDict == null)
				xmlTableDict = new Dictionary<string, List<ExcelItem>>();

			xmlTableDict.Clear();


			if (jsonTableDict == null)
				jsonTableDict = new Dictionary<string, List<ExcelItem>>();

			jsonTableDict.Clear();

			if (luaTableDict == null)
				luaTableDict = new Dictionary<string, List<ExcelItem>>();

			luaTableDict.Clear();


			if (sqlTableDict == null)
				sqlTableDict = new Dictionary<string, List<ExcelItem>>();

			sqlTableDict.Clear();
		}


		/// <summary>
		/// 初始化
		/// </summary>
		public void Init()
		{			
			LoadCSV<UserLevel>("UserLevel");
			LoadXMLTable<UserLevel>("UserLevel");
			LoadJsonTable<UserLevel>("UserLevel");
			LoadSQLTable<UserLevel>("UserLevel");
		}

		#region CSV表的加载、卸载、表数据获取		
		/// <summary>
		/// 加载一CSV文件中所有数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public void LoadCSV<T>(string fileName) where T:ExcelItem 
		{
			if (csvTableDict.ContainsKey(fileName))
			{
				Debug.LogError("CSV表："+fileName+"已经加载，无需重复加载！");
				return;
			}

			string fileFullName = fileName + ".csv";
			string filePath = PathUtil.GetCSVDir(fileFullName) + "/"+ fileFullName;
			List<T> excelItems = CSVHelper.ReadCSVFile<T>(filePath);
			List<ExcelItem> commomItems = new List<ExcelItem>();
			for (int i = 0; i < excelItems.Count; i++)
			{
				commomItems.Add((ExcelItem)excelItems[i]);
			}
			
			csvTableDict.Add(fileName, commomItems);
			//Debug.Log("wlq====>CSV表加载完毕");
		}

		/// <summary>
		/// 获取某张CSV配置表的所有数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public List<T> GetCSV<T>(string fileName)  where T:ExcelItem
		{
			List<ExcelItem> commonItems = null;
			List<T> resItems = null;
			if (csvTableDict.TryGetValue(fileName, out commonItems))
			{
				resItems = new List<T>();

				for (int i = 0; i < commonItems.Count; i++)
				{
					resItems.Add((T)commonItems[i]);
				}
			}

			if (resItems == null)
				Debug.LogError("CSV表 "+fileName+"未加载，不能获取该表的所有数据");

			return resItems;
		}

		/// <summary>
		/// 获取某张CSV表的某一行数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public T GetCSVRowData<T>(string fileName, string id) where T:ExcelItem
		{
			T resItem = null;

			List<ExcelItem> commonItems = null;
			if (csvTableDict.TryGetValue(fileName, out commonItems))
			{
				ExcelItem excelItem = commonItems.Find(t => t.ID == id);

				if (excelItem != null)
				{
					resItem = (T)excelItem;
				}
			}

			if (resItem == null)
				Debug.LogError("CSV表 "+fileName+" ID="+id+" 不存在 原因表未加载或者没有该ID的行数据");

			return resItem;
		}

		/// <summary>
		/// 卸载CSV表数据
		/// </summary>
		/// <param name="fileName"></param>
		public void UnLoadCSV(string fileName)
		{
			if (csvTableDict.ContainsKey(fileName))
			{
				csvTableDict.Remove(fileName);
			}
		}
        #endregion

        #region XML表的加载、卸载以及表数据的获取
        /// <summary>
        /// 加载一xml文件中所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public void LoadXMLTable<T>(string fileName)	where T:ExcelItem
		{
			if (xmlTableDict.ContainsKey(fileName))
			{
				Debug.Log("XML表：" + fileName + "已经加载，无需重复加载！");
				return;
			}

			string fileFullName = fileName + ".xml";
			string filePath = PathUtil.GetXmlDir(fileFullName) + "/"+ fileFullName;
			List<T> excelItems = XmlHelper.ReadXmlFile<T>(filePath);
			List<ExcelItem> commonItems = new List<ExcelItem>();
			for (int i = 0; i < excelItems.Count; i++)
			{
				commonItems.Add((ExcelItem)excelItems[i]);
			}

			xmlTableDict.Add(fileName, commonItems);

			//Debug.Log("wlq====>xml表加载完毕");
		}

		/// <summary>
		/// 获取某张xml表的所有数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public List<T> GetXMLTable<T>(string fileName) where T:ExcelItem
		{
			List<ExcelItem> commonItems = null;
			List<T> resItems = null;
			if (xmlTableDict.TryGetValue(fileName, out commonItems))
			{
				resItems = new List<T>();

				for (int i = 0; i < commonItems.Count; i++)
				{
					resItems.Add((T)commonItems[i]);
				}
			}

			if (resItems == null)
				Debug.LogError("XML表 " + fileName + "未加载，不能获取该表的所有数据");

			return resItems;
		}

		/// <summary>
		/// 获取某张XML表中的某行数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public T GetXmlTableRowData<T>(string fileName, string id) where T:ExcelItem
		{
			T resItem = null;

			List<ExcelItem> commonItems = null;
			if (xmlTableDict.TryGetValue(fileName, out commonItems))
			{
				ExcelItem excelItem = commonItems.Find(t => t.ID == id);

				if (excelItem != null)
				{
					resItem = (T)excelItem;
				}
			}

			if (resItem == null)
				Debug.LogError("XML表 " + fileName + " ID=" + id + " 不存在 原因表未加载或者没有该ID的行数据");

			return resItem;
		}


		/// <summary>
		/// 卸载某张xml表数据
		/// </summary>
		/// <param name="fileName"></param>
		public void UnLoadXMLTable(string fileName)
		{
			if (xmlTableDict.ContainsKey(fileName))
			{
				xmlTableDict.Remove(fileName);
			}
		}
        #endregion

        #region Json表的加载、卸载以及表数据的获取
        /// <summary>
        /// 加载一Json文件中所有数据
        /// </summary>
        /// <param name="fileName"></param>
        public void LoadJsonTable<T>(string fileName) where T:ExcelItem
		{
			if (jsonTableDict.ContainsKey(fileName))
			{
				Debug.Log("Json表：" + fileName + "已经加载，无需重复加载！");
				return;
			}

			string fileFullName = fileName + ".json";
			string filePath = PathUtil.GetJsonDir(fileFullName) + "/"+ fileFullName;
			List<T> excelItems =  JsonHelper.ReadJsonFile<T>(filePath);
			List<ExcelItem> commonItems = new List<ExcelItem>();
			for (int i = 0; i < excelItems.Count; i++)
			{
				commonItems.Add((ExcelItem)excelItems[i]);
			}

			jsonTableDict.Add(fileName,commonItems);

			//Debug.Log("wlq====>json表加载完毕");
		}

		/// <summary>
		/// 获取某张Json表的所有数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public List<T> GetJsonTable<T>(string fileName) where T : ExcelItem
		{
			List<ExcelItem> commonItems = null;
			List<T> resItems = null;
			if (jsonTableDict.TryGetValue(fileName, out commonItems))
			{
				resItems = new List<T>();

				for (int i = 0; i < commonItems.Count; i++)
				{
					resItems.Add((T)commonItems[i]);
				}
			}

			if (resItems == null)
				Debug.LogError("Json表 " + fileName + "未加载，不能获取该表的所有数据");

			return resItems;
		}


		/// <summary>
		/// 获取某张Json表中某行数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public T GetJsonTableRowData<T>(string fileName, string id) where T : ExcelItem
		{
			T resItem = null;

			List<ExcelItem> commonItems = null;
			if (jsonTableDict.TryGetValue(fileName, out commonItems))
			{
				ExcelItem excelItem = commonItems.Find(t => t.ID == id);

				if (excelItem != null)
				{
					resItem = (T)excelItem;
				}
			}

			if (resItem == null)
				Debug.LogError("Json表 " + fileName + " ID=" + id + " 不存在 原因表未加载或者没有该ID的行数据");

			return resItem;
		}

		/// <summary>
		/// 卸载某张Json表
		/// </summary>
		/// <param name="fileName"></param>
		public void UnLoadJsonTable(string fileName)
		{
			if (jsonTableDict.ContainsKey(fileName))
			{
				jsonTableDict.Remove(fileName);
			}
		}
        #endregion
        
        #region SQL表的加载、卸载以及表数据的读取
        /// <summary>
        /// 加载某张数据库表中数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        public void LoadSQLTable<T>(string fileName) where T : ExcelItem
		{
			if (sqlTableDict.ContainsKey(fileName))
			{
				Debug.LogError("SQL表：" + fileName + "已经加载，无需重复加载！");
				return;
			}

			List<T> excelItems = SQLHelper.ReadSQLFile<T>(fileName);

			List<ExcelItem> commomItems = new List<ExcelItem>();
			for (int i = 0; i < excelItems.Count; i++)
			{
				commomItems.Add((ExcelItem)excelItems[i]);
			}

			sqlTableDict.Add(fileName, commomItems);

			//Debug.Log("wlq====>sql表加载完毕");
		}


		/// <summary>
		/// 获取SQL表中的所有数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public List<T> GetSQLTable<T>(string fileName) where T : ExcelItem
		{
			List<ExcelItem> commonItems = null;
			List<T> resItems = null;
			if (sqlTableDict.TryGetValue(fileName, out commonItems))
			{
				resItems = new List<T>();

				for (int i = 0; i < commonItems.Count; i++)
				{
					resItems.Add((T)commonItems[i]);
				}
			}

			if (resItems == null)
				Debug.LogError("SQL表 " + fileName + "未加载，不能获取该表的所有数据");

			return resItems;
		}


		/// <summary>
		/// 获取Lua表中某行数据
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public T GetSQLTableRowData<T>(string fileName, string id) where T : ExcelItem
		{
			T resItem = null;

			List<ExcelItem> commonItems = null;
			if (sqlTableDict.TryGetValue(fileName, out commonItems))
			{
				ExcelItem excelItem = commonItems.Find(t => t.ID == id);

				if (excelItem != null)
				{
					resItem = (T)excelItem;
				}
			}

			if (resItem == null)
				Debug.LogError("SQL表 " + fileName + " ID=" + id + " 不存在 原因表未加载或者没有该ID的行数据");

			return resItem;
		}

		/// <summary>
		/// 卸载某张SQL配置表
		/// </summary>
		/// <param name="fileName"></param>
		public void UnLoadSQLTable(string fileName)
		{
			if (sqlTableDict.ContainsKey(fileName))
			{
				sqlTableDict.Remove(fileName);
			}
		}
        #endregion

    }
}

